package com.itheima.reggin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.User;

import com.itheima.reggin.service.UserService;
import com.itheima.reggin.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: HShuai
 * @Date: 2022/07/13/9:49星期三
 * @Description: 移动端用户
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送短信
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        //获取用户手机号码
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            //手机号码不为空 生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码:{}",code);

            //调用阿里云提供的短信服务API完成发送短信
            // SMSUtils.sendMessage("阿里云短信测试", "SMS_154950909", phone, code);
            //设置验证码缓存存活时间5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("验证码发送成功");
        }
        return R.error("验证码发送失败");
    }


    /**
     * 登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody Map map, HttpSession session){
        //获取手机号码
        String phone = (String) map.get("phone");
        //获取验证码
        String code = (String) map.get("code");
        //从缓存中读取验证码
        String codeRedis = (String) redisTemplate.opsForValue().get(phone);
        log.info(codeRedis);
        if (codeRedis !=null && codeRedis.equals(code)){
            //验证码输入正确
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone,phone);
            User user = userService.getOne(lqw);
            //判断user是否为空 为空就注册新用户
            if (user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            //登录成功 将用户userId传到session域中
            session.setAttribute("user",user.getId());
            //删除缓存的验证码
            redisTemplate.delete(phone);
            return R.success("登录成功");

        }

        return R.error("登录失败");
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("loginout")
    public R<String > loginOut(HttpSession session){
        session.invalidate(); //销毁session域中信息
         return R.success("退出登录");
    }


}
